package module.map.topButtons
{
	import drongo.desktop.utils.UtilsFile;
	import drongo.model.events.ModelEvent;
	
	import flash.events.Event;
	import flash.filesystem.File;
	
	import common.view.FGUIMediator;
	
	import module.map.ImageResize.ImageResizeWindow;
	import module.map.model.MapModel;
	import module.map.model.ProjectModel;
	import module.map.model.Propertys;
	import module.map.utils.ExportUtils;
	
	import robotlegs.bender.extensions.mediatorMap.api.IMediatorMap;
	
	import ui.Map.UI_TopButtonPanel;
	
	public class TopButtonMediator extends FGUIMediator
	{
		[Inject]
		public var view:UI_TopButtonPanel;
		
		[Inject]
		public var project:ProjectModel;
		[Inject]
		public var mediatorMap:IMediatorMap;
		private var __currentMap:MapModel;
		public function TopButtonMediator()
		{
			super();
		}
		
		override public function initialize():void{
			super.initialize();
			this.currentMap=this.project.currentMap;
			this.AddEvents();
			this.RefreshView();
		}
		
		override public function destroy():void{
			super.destroy();
			this.RemoveEvents();
		}
		
		private function set currentMap(value:MapModel):void{
			if(__currentMap){
				__currentMap.removeEventListener(ModelEvent.CHILD_VALUE_CHANGED,this.MapPropertyChanged);
			}
			__currentMap=value;
			if(__currentMap){
				__currentMap.addEventListener(ModelEvent.CHILD_VALUE_CHANGED,this.MapPropertyChanged);
			}
		}
		
		private function MapPropertyChanged(e:ModelEvent):void{
			RefreshView();
		}
		
		private function RefreshView():void{
			if(this.__currentMap==null){
				return;
			}
			var state:uint=this.__currentMap.Get(Propertys.TEST).GetValue();
			this.view.m_btnRun.selected=state>0;
		}
		
		private function AddEvents():void{
			this.view.m_btnRun.addClickListener(this.RunTestButtonClick);
			this.view.m_resizeBtn.addClickListener(this.ResizeButtonClick);
			this.project.addEventListener(Event.CHANGE,this.SwicthMapHandler);
			this.view.m_btnExport.addClickListener(this.ExportButtonClick);
		}
		
		private function RemoveEvents():void{
			this.view.m_btnRun.removeClickListener(this.RunTestButtonClick);
			this.view.m_resizeBtn.removeClickListener(this.ResizeButtonClick);
			this.project.removeEventListener(Event.CHANGE,this.SwicthMapHandler);
			this.view.m_btnExport.removeClickListener(this.ExportButtonClick);
		}
		
		private function SwicthMapHandler(e:Event):void{
			this.currentMap=this.project.currentMap;
			this.RefreshView();
		}
		
		private function ResizeButtonClick(e:Event):void{
			var window:ImageResizeWindow=new ImageResizeWindow();
			window.showOn(this.view.root);
			window.centerOn(this.view.root);
			mediatorMap.mediate(window);
		}
		
		private function ExportButtonClick(e:Event):void{
			if(this.project.currentMap==null){
				return;
			}
			UtilsFile.browseForSave("导出",this.ExportSaveSelect);
		}
		
		private function ExportSaveSelect(file:File):void{
			ExportUtils.ExportMap(file,this.project,false);
		}
		
		private function RunTestButtonClick(e:Event):void{
			if(this.__currentMap==null){
				return;
			}
			var state:uint=this.__currentMap.Get(Propertys.TEST).GetValue();
			if(state==1){
				this.__currentMap.Update(Propertys.TEST,0);
			}else{
				this.__currentMap.Update(Propertys.TEST,1);
			}
		}
	}
}